package com.tiny.diamond;

import com.tiny.diamond.internal.parser.json.ObjectJsonParser;

import java.security.Security;
import java.util.HashMap;
import java.util.Map;

public class DefaultDiamondClient implements DiamondClient {

    private String serverUrl;

    private String appId;

    private String privateKey;

    private String prodCode;

    /**
     * 格式化类型
     */
    private String format = DiamondConstants.FORMAT_JSON;

    /**
     * 签名类型
     */
    private String signType = DiamondConstants.SIGN_TYPE;

    /**
     * 加密类型
     */
    private String encryptType = DiamondConstants.ENCRYPT_TYPE_AES;

    private String encryptKey;

    private String tinyPublicKey;

    private String charset;

    private int connectTimeout = 3000;
    private int readTimeout = 15000;

    private String proxyHost;
    private int proxyPort;

    static {
        //清除安全设置
        Security.setProperty("jdk.certpath.disabledAlgorithms", "");
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey) {
        this.serverUrl = serverUrl;
        this.appId = appId;
        this.privateKey = privateKey;
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey, String format) {
        this(serverUrl, appId, privateKey);
        this.format = format;
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey, String format, String charset) {
        this(serverUrl, appId, privateKey, format);
        this.charset = charset;
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey, String format,
                                String charset, String tinyPublicKey) {
        this(serverUrl, appId, privateKey, format, charset);
        this.tinyPublicKey = tinyPublicKey;
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey, String format,
                                String charset, String tinyPublicKey, String signType) {
        this(serverUrl, appId, privateKey, format, charset, tinyPublicKey);
        this.signType = signType;
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey, String format,
                                String charset, String tinyPublicKey, String signType,
                                String proxyHost, int proxyPort) {
        this(serverUrl, appId, privateKey, format, charset, tinyPublicKey, signType);
        this.proxyHost = proxyHost;
        this.proxyPort = proxyPort;
    }

    public DefaultDiamondClient(String serverUrl, String appId, String privateKey, String format,
                                String charset, String tinyPublicKey, String signType,
                                String encryptKey, String encryptType) {
        this(serverUrl, appId, privateKey, format, charset, tinyPublicKey, signType);
        this.encryptKey = encryptKey;
        this.encryptType = encryptType;
    }

    public static Builder builder(String serverUrl, String appId, String privateKey) {
        return new Builder(serverUrl, appId, privateKey);
    }

    @Override
    public <T extends DiamondResponse> T execute(DiamondRequest<T> request) throws DiamondApiException {
        return execute(request, null);
    }

    @Override
    public <T extends DiamondResponse> T execute(DiamondRequest<T> request, String authToken) throws DiamondApiException {
        return execute(request, authToken, null);
    }

    @Override
    public <T extends DiamondResponse> T execute(DiamondRequest<T> request, String authToken, String appAuthToken) throws DiamondApiException {

        DiamondParser<T> parser = null;
        if (DiamondConstants.FORMAT_XML.equals(this.format)) {

        } else {
            parser = new ObjectJsonParser<T>(request.getResponseClass());
        }

        return null;
    }

    @Override
    public <T extends DiamondResponse> T pageExecute(DiamondRequest<T> request) throws DiamondApiException {
        return null;
    }

    @Override
    public <T extends DiamondResponse> T pageExecute(DiamondRequest<T> request, String method) throws DiamondApiException {
        return null;
    }

    @Override
    public <T extends DiamondResponse> T sdkExecute(DiamondRequest<T> request) throws DiamondApiException {
        return null;
    }

    @Override
    public <TR extends DiamondResponse, T extends DiamondRequest<TR>> TR parseAppSyncResult(Map<String, String> result, Class<T> requsetClazz) throws DiamondApiException {
        return null;
    }

    private <T extends DiamondResponse> T _execute(DiamondRequest<T> request, DiamondParser<T> parser, String authToken, String appAuthToken) throws DiamondApiException {

        // TODO
        return null;
    }

    private <T extends DiamondResponse> Map<String , Object> doPost(DiamondRequest<T> request,String accessToken,String appAuthToken)throws DiamondApiException{
        Map<String, Object> result = new HashMap<>();
        // TODO
        return null;
    }



    public static class Builder {
        private DefaultDiamondClient client;

        Builder(String serverUrl, String appId, String privateKey) {
            client = new DefaultDiamondClient(serverUrl, appId, privateKey);
        }

        public DefaultDiamondClient build() {
            return client;
        }

        public Builder proCode(String proCode) {
            client.setProdCode(proCode);
            return this;
        }

        public Builder format(String format) {
            client.setFormat(format);
            return this;
        }

        public Builder signType(String signType) {
            client.setSignType(signType);
            return this;
        }

        public Builder encryptType(String encryptType) {
            client.setEncryptType(encryptType);
            return this;
        }

        public Builder encryptKey(String encryptKey) {
            client.setEncryptKey(encryptKey);
            return this;
        }

        public Builder tinyPublicKey(String tinyPublicKey) {
            client.setTinyPublicKey(tinyPublicKey);
            return this;
        }

        public Builder charset(String charset) {
            client.setCharset(charset);
            return this;
        }

        public Builder connectTimeout(int connectTimeout) {
            client.setConnectTimeout(connectTimeout);
            return this;
        }

        public Builder readTimeout(int readTimeout) {
            client.setReadTimeout(readTimeout);
            return this;
        }

        public Builder proxyHost(String proxyHost) {
            client.setProxyHost(proxyHost);
            return this;
        }

        public Builder proxyPort(int proxyPort) {
            client.setProxyPort(proxyPort);
            return this;
        }

    }

    public void setProdCode(String prodCode) {
        this.prodCode = prodCode;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public void setEncryptType(String encryptType) {
        this.encryptType = encryptType;
    }

    public void setEncryptKey(String encryptKey) {
        this.encryptKey = encryptKey;
    }

    public void setTinyPublicKey(String tinyPublicKey) {
        this.tinyPublicKey = tinyPublicKey;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public void setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
    }
}
